package com.demo.compute.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by DongGuangqing on 2016/10/3.
 */

@RestController
public class ComputerController {
    private final Logger logger = Logger.getLogger(getClass());

    //获取当前服务实例的相关内容
    @Autowired
    private DiscoveryClient client;

    @ApiOperation(value="计算加法", notes="") //给API增加说明
    @ApiImplicitParams({ // 通过@ApiImplicitParams、@ApiImplicitParam注解来给参数增加说明
            @ApiImplicitParam(paramType = "query" , name = "a", value = "输入参数a", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query" , name = "b", value = "输入参数b", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "body" , name = "c", value = "输入参数c", required = true, dataType = "Swagger2")
    })
    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        ServiceInstance instance = client.getLocalServiceInstance();
        Integer r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return r;
    }
}
